package com.hq.ims.data.service;

import com.hq.ims.data.dao.CompanyDepartmentMapper;
import com.hq.ims.data.entity.Company;
import com.hq.ims.data.entity.CompanyDepartment;
import com.hq.ims.data.entity.CompanyDepartmentEmployee;
import com.hq.ims.data.entity.PermissionMapAuthorityDepartment;
import com.hq.ims.data.vo.CompanyDepartmentMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.utils.util.BooleanUtils;
import com.hq.utils.util.ListUtils;
import com.hq.utils.util.StringUtils;
import lombok.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * jiang bai ping
 *
 * @Email 526666031@qq.com
 * @Detail 公司部门服务实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BaseCompanyDepartmentService extends BaseService<CompanyDepartmentMapper, CompanyDepartment> {

    @Resource
    private BaseCompanyDepartmentEmployeeService baseCompanyMapDepartmentUserService;
    @Resource
    private BaseCompanyService baseCompanyService;
    @Resource
    private BasePermissionMapAuthorityUserService basePermissionMapGroupUserService;
    @Resource
    private BasePermissionAuthorityService basePermissionAuthorityService;
    @Resource
    private BasePermissionMapAuthorityRoleService basePermissionMapGroupRoleService;
    @Resource
    private BasePermissionMapAuthorityDepartmentService basePermissionMapAuthorityDepartmentService;
    @Resource
    private BaseCompanyDepartmentEmployeeService baseCompanyDepartmentEmployeeService;

    /**
     * 得到部门按公司id列表
     *
     * @param companyId 公司ID
     */
    public List<CompanyDepartment> getByCompanyId(@NonNull String companyId) {
        return this.lambdaQuery()
                .eq(CompanyDepartment::getCompanyId, companyId)
                .orderByAsc(CompanyDepartment::getCreateDateTime)
                .list();
    }

    /**
     * 添加部门
     */
    @Transactional(rollbackFor = Exception.class)
    public CompanyDepartment addDepartment(CompanyDepartmentMapstruct.SaveVo vo) {
        Company company = baseCompanyService.getById(vo.getCompanyId());
        if (company == null) {
            return null;
        }

        CompanyDepartment companyDepartment = this.getByCompanyIdAndCompanyDepartmentName(vo.getCompanyId(), vo.getCompanDepartmentName(), null);
        if (companyDepartment != null) {
            return companyDepartment;
        }
        //账值新的对象
        CompanyDepartment entity = new CompanyDepartment();
        entity.setCompanyId(company.getCompanyId());
        entity.setCompanyName(company.getCompanyName());
        entity.setCompanyDepartmentName(vo.getCompanDepartmentName());
        entity.insert();
        return entity;
    }

    /**
     * 根据公司ID和部门名称获取部门
     *
     * @param companyId             公司ID
     * @param companyDepartmentName 部门名称
     * @param needExcludeId         需要排除的ID
     */
    public CompanyDepartment getByCompanyIdAndCompanyDepartmentName(
            String companyId,
            String companyDepartmentName,
            String needExcludeId
    ) {
        List<CompanyDepartment> list = this.lambdaQuery().eq(CompanyDepartment::getCompanyId, companyId)
                .eq(CompanyDepartment::getCompanyDepartmentName, companyDepartmentName)
                .ne(StringUtils.isNotEmpty(needExcludeId), CompanyDepartment::getCompanyDepartmentId, needExcludeId)
                .list();
        if (ListUtils.isNotExistsEle(list)) {
            return null;
        }
        return list.get(0);
    }

    /**
     * 添加部门
     */
    @Transactional(rollbackFor = Exception.class)
    public CompanyDepartment createDefaultDepartment(
            @NonNull String companyId
    ) {
        CompanyDepartmentMapstruct.SaveVo createDepartmentVo = new CompanyDepartmentMapstruct.SaveVo();
        createDepartmentVo.setCompanDepartmentName("默认部门");
        createDepartmentVo.setCompanyId(companyId);
        return this.addDepartment(createDepartmentVo);
    }


    /**
     * 删除当前部门以及子节点部门
     */
    public void deleteDepartment(
            @NonNull String companyDepartmentId,
            @NonNull String companyId
    ) {
        //检查是否有当前部门
        CompanyDepartment companyDepartment = this.getByCompanyIdAndDepartmentId(companyDepartmentId, companyId);
        if (companyDepartment == null) {
            return;
        }
        boolean isExistsDepartmentEmployee = baseCompanyDepartmentEmployeeService.getEmployeeCountByDepartmentId(companyDepartmentId) > 0;
        BooleanUtils.isTrueThrow(isExistsDepartmentEmployee, "该部门下还存在员工请先将员工调走");
        //删除部门下的成员
        baseCompanyMapDepartmentUserService.lambdaUpdate().eq(CompanyDepartmentEmployee::getCompanyDepartmentId, companyDepartmentId).remove();
        //删除部门权限模版
        basePermissionMapAuthorityDepartmentService.lambdaUpdate().eq(PermissionMapAuthorityDepartment::getCompanyDepartmentId, companyDepartmentId).remove();
        //删除部门
        companyDepartment.deleteById();
    }

    public CompanyDepartment getByCompanyIdAndDepartmentId(
            @NonNull String companyDepartmentId,
            @NonNull String companyId
    ) {
        List<CompanyDepartment> list = this.lambdaQuery().eq(CompanyDepartment::getCompanyDepartmentId, companyDepartmentId)
                .eq(CompanyDepartment::getCompanyId, companyId)
                .list();
        if (ListUtils.isNotExistsEle(list)) {
            return null;
        }
        return list.get(0);
    }

    /**
     * 更新部门
     */
    public Boolean updateDepartment(CompanyDepartmentMapstruct.SaveVo vo) {
        //账值新的对象
        CompanyDepartment entity = this.getById(vo.getCompanyDepartmentId());
        if (entity == null) {
            return false;
        }
        entity.setCompanyDepartmentName(vo.getCompanDepartmentName());
        return entity.updateById();
    }


    /**
     * 是存在部门名称
     *
     * @param companyId      公司标识
     * @param departmentName 部门名称
     * @param needExcludeId  需要排除的部门ID
     * @return boolean
     */
    public boolean isExistDepartmentName(
            @NonNull String companyId,
            @NonNull String departmentName,
            String needExcludeId
    ) {
        return this.lambdaQuery()
                .eq(CompanyDepartment::getCompanyId, companyId)
                .eq(CompanyDepartment::getCompanyDepartmentName, departmentName.trim())
                .ne(StringUtils.isNotEmpty(needExcludeId), CompanyDepartment::getCompanyDepartmentId, needExcludeId)
                .count() > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    public void delByCompanyId(String companyId) {
        this.lambdaUpdate().eq(CompanyDepartment::getCompanyId, companyId).remove();
    }
}
